package cn.wolfcode.car.business.web.controller;


import cn.wolfcode.car.business.domain.Salary;
import cn.wolfcode.car.business.query.SalaryQueryObject;
import cn.wolfcode.car.business.service.ISalaryService;
import cn.wolfcode.car.common.base.page.TablePageInfo;
import cn.wolfcode.car.common.web.AjaxResult;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 岗位控制器
 */
@Controller
@RequestMapping("business/salary")
public class SalaryController {
    //模板前缀
    private static final String prefix = "business/salary/";

    @Autowired
    private ISalaryService salaryService;

    //页面------------------------------------------------------------
    //列表
    @RequiresPermissions("business:salary:view")
    @RequestMapping("/listPage")
    public String listPage() {
        return prefix + "list";
    }

    @RequiresPermissions("business:salary:add")
    @RequestMapping("/addPage")
    public String addPage() {
        return prefix + "add";
    }


    @RequiresPermissions("business:salary:edit")
    @RequestMapping("/editPage")
    public String editPage(Long id, Model model) {
        model.addAttribute("salary", salaryService.get(id));
        return prefix + "edit";
    }

    //数据-----------------------------------------------------------
    //列表
    @RequiresPermissions("business:salary:list")
    @RequestMapping("/query")
    @ResponseBody
    public TablePageInfo<Salary> query(SalaryQueryObject qo) {
        return salaryService.query(qo);
    }

    //新增
    @RequiresPermissions("business:salary:add")
    @RequestMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Salary salary) {
        salaryService.save(salary);
        return AjaxResult.success();
    }

    //编辑
    @RequiresPermissions("business:salary:edit")
    @RequestMapping("/edit")
    @ResponseBody
    public AjaxResult edit(Salary salary) {
        salaryService.update(salary);
        return AjaxResult.success();
    }

    //删除
    @RequiresPermissions("business:salary:remove")
    @RequestMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        salaryService.deleteBatch(ids);
        return AjaxResult.success();
    }

    //下载
    @RequestMapping("/exportXls")
    public void exportXls(HttpServletResponse response) throws IOException {
        Workbook wb = new XSSFWorkbook();
        Sheet sheet = wb.createSheet("客户列表");
        Row row = sheet.createRow(0);
        row.createCell(0).setCellValue("id");
        row.createCell(1).setCellValue("员工姓名");
        row.createCell(2).setCellValue("基础工资");
        row.createCell(3).setCellValue("绩效工资");
        row.createCell(4).setCellValue("扣款");
        row.createCell(5).setCellValue("实际发放");
        row.createCell(6).setCellValue("发放方式");
        row.createCell(7).setCellValue("备注");
        row.createCell(8).setCellValue("发放时间");
        List<Salary> salaryList = salaryService.list();
        for (int i = 0; i < salaryList.size(); i++) {
            Row row1 = sheet.createRow(i + 1);
            row1.createCell(0).setCellValue(salaryList.get(i).getId());
            row1.createCell(1).setCellValue(salaryList.get(i).getName());
            row1.createCell(2).setCellValue(salaryList.get(i).getBasicSalary().toString());
            row1.createCell(3).setCellValue(salaryList.get(i).getMeritSalary().toString());
            row1.createCell(4).setCellValue(salaryList.get(i).getDeductMoney().toString());
            row1.createCell(5).setCellValue(salaryList.get(i).getRealSalary().toString());
            row1.createCell(6).setCellValue(salaryList.get(i).getPaySource());
            row1.createCell(7).setCellValue(salaryList.get(i).getInfo());
            row1.createCell(8).setCellValue(salaryList.get(i).getDate());

        }
        response.setHeader("Content-Disposition", "attachment; filename ="
                + java.net.URLEncoder.encode("客户名单.xlsx", "UTF-8"));
        //客户端浏览器输出流
        ServletOutputStream outputStream = response.getOutputStream();
        wb.write(outputStream);
        wb.close();
        outputStream.close();
    }


}
